Strict

Import "host.bmx"

Const SPLITWIDTH=6

Type TSplitter Extends TEventHandler
	Const	HORIZONTAL=0
	Const	VERTICAL=1
	Const	ISFLIPPED=1
	Const	ISFIXED=2
	
	Field	axis,fixed
	Field	gadget0:TGadget
	Field	gadget1:TGadget
	Field	group:TGadget
	Field	panel:TGadget
	Field	pos,flipped,snap,oldpos,oldsize
	
	Method Position(p)
		Move p-pos
	End Method
		
	Method Move(d)
		Local	x,y,w,h,p
		pos:+d
		w=ClientWidth(group)
		h=ClientHeight(group)
		If pos<0 pos=0
		If axis=VERTICAL
			If pos>w-SPLITWIDTH pos=w-SPLITWIDTH
			y=Min(gadget0.ypos,gadget1.ypos)
			h=Max(gadget0.height+gadget0.ypos,gadget1.height+gadget1.ypos)-y
			If (d<0)~flipped
				SetGadgetShape gadget0,0,y,pos-SPLITWIDTH,h
				SetGadgetShape panel,pos-SPLITWIDTH,y,SPLITWIDTH*2,h
				SetGadgetShape gadget1,pos+SPLITWIDTH,y,w-(pos+SPLITWIDTH),h
			Else
				SetGadgetShape gadget1,pos+SPLITWIDTH,y,w-(pos+SPLITWIDTH),h
				SetGadgetShape panel,pos-SPLITWIDTH,y,SPLITWIDTH*2,h
				SetGadgetShape gadget0,0,y,pos-SPLITWIDTH,h
			EndIf
		Else
			If pos>h-SPLITWIDTH pos=h-SPLITWIDTH
			If (d<0)~flipped
				SetGadgetShape gadget0,0,0,w,pos-SPLITWIDTH
				SetGadgetShape panel,0,pos-SPLITWIDTH,w,SPLITWIDTH*2
				SetGadgetShape gadget1,0,pos+SPLITWIDTH,w,h-(pos+SPLITWIDTH)
			Else
				SetGadgetShape gadget1,0,pos+SPLITWIDTH,w,h-(pos+SPLITWIDTH)
				SetGadgetShape panel,0,pos-SPLITWIDTH,w,SPLITWIDTH*2
				SetGadgetShape gadget0,0,0,w,pos-SPLITWIDTH
			EndIf
		EndIf
	End Method
	
	Method Resize()
		If axis=VERTICAL
			pos=GadgetX(panel)+SPLITWIDTH
			If pos<0 Or pos>ClientWidth(group) move(0)
		Else
			pos=GadgetY(panel)+SPLITWIDTH
			If pos<0 Or pos>ClientHeight(group) move(0)
		EndIf
	End Method		
	
	Method OnEvent()
		If EventSource()<>panel Return
		Select EventID()
			Case EVENT_MOUSEDOWN
				snap=True
				If axis
					oldpos=EventX()
				Else
					oldpos=EventY()
				EndIf				
			Case EVENT_MOUSEENTER
				If axis
					SetPointer POINTER_SIZEWE
				Else
					SetPointer POINTER_SIZENS
				EndIf
			Case EVENT_MOUSELEAVE
				SetPointer POINTER_ARROW
			Case EVENT_MOUSEUP
				snap=False
			Case EVENT_MOUSEMOVE
				If snap
					If axis
						Move EventX()-oldpos
					Else
						Move EventY()-oldpos
					EndIf
				EndIf			
		End Select
	End Method
	
	Method SetFlip(f,init=True)
		Local	t:TGadget
		If f<>flipped
			flipped=f
			t=gadget0;gadget0=gadget1;gadget1=t
			If init 
				If axis=VERTICAL
					pos=ClientWidth(group)-pos-1
				Else
					pos=ClientHeight(group)-pos-1
				EndIf
			EndIf
		EndIf			
		If axis
			If flipped
				SetGadgetLayout gadget0,1,0,1,1
				SetGadgetLayout gadget1,1,1,1,1
				SetGadgetLayout panel,1,0,1,1
			Else
				SetGadgetLayout gadget0,1,1,1,1
				SetGadgetLayout gadget1,0,1,1,1
				SetGadgetLayout panel,0,1,1,1
			EndIf
		Else
			If flipped
				SetGadgetLayout gadget0,1,1,1,0
				SetGadgetLayout gadget1,1,1,1,1
				SetGadgetLayout panel,1,1,1,0
			Else
				SetGadgetLayout gadget0,1,1,1,1
				SetGadgetLayout gadget1,1,1,0,1
				SetGadgetLayout panel,1,1,0,1
			EndIf
		EndIf
		Position pos
	End Method

	Function Create:TSplitter(axis,pos,flags,gadget0:TGadget,gadget1:TGadget,group:TGadget)
		Local	s:TSplitter		
		s=New TSplitter
		s.axis=axis
		s.gadget0=gadget0
		s.gadget1=gadget1
		s.group=group	'GadgetGroup(gadget0)
		s.panel=CreatePanel(0,0,0,0,s.group,PANEL_ACTIVE)'|PANEL_BORDER)
		SetGadgetColor s.panel,230,230,230
		s.pos=pos
		s.setflip flags&ISFLIPPED,False
		If flags&ISFIXED s.fixed=True
		Return s
	End Function
End Type

